﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Faust.Andrew.MTG.Player
{
    static class ServerHelper
    {
        public static List<int> ToIDList(this List<Shared.GameCard> cards)
        {
            List<int> ids = new List<int>();

            foreach (var card in cards)
            {
                ids.Add(card.CardID);
            }

            return ids;
        }

        public static bool AreCountersUpdated(ObservableCollection<Shared.Counter> originalCounters, ObservableCollection<Shared.Counter> newCounters)
        {
            if (originalCounters == null && newCounters != null) { return true; }
            if (originalCounters != null && newCounters == null) { return true; }
            if (originalCounters == null && newCounters == null) { return false; }
            if (originalCounters.Count != newCounters.Count) { return true; }

            foreach (Shared.Counter counter in newCounters)
            {
                int count = originalCounters.Count(tempCounter => tempCounter.Name == counter.Name && tempCounter.Value == counter.Value);

                if (count == 0) { return true; }
            }

            return false;
        }

        public static void Shuffle(List<Shared.GameCard> cards, Random rand)
        {
            Shared.GameCard tempCard;
            int tempPos = 0;
            //int tempPos2 = 0;

            //for (int i = 0; i < 500; i++)
            //{
            //    tempPos = rand.Next(0, cards.Count);
            //    tempPos2 = rand.Next(0, cards.Count);

            //    tempCard = cards[tempPos];
            //    cards[tempPos] = cards[tempPos2];
            //    cards[tempPos2] = tempCard;
            //}            

            for (int i = 0; i < 500; i++)
            {
                for (int j = 0; j < cards.Count; j++)
                {
                    tempPos = rand.Next(0, cards.Count);
                    tempCard = cards[j];
                    cards[j] = cards[tempPos];
                    cards[tempPos] = tempCard;
                }
            }
        }

        public static string GetPublicCardName(Shared.GameCard card)
        {
            return GetPublicCardName(card, card.Location.Zone);
        }

        public static string GetPublicCardName(Shared.GameCard card, Shared.Zone newZone)
        {
           
            //If the card is in a public zone and not morphed we return the name. Otherwise
            //we just inform that it is a card.
            if (card.Location.Zone == Faust.Andrew.MTG.Player.Shared.Zone.InPlay
                || card.Location.Zone == Faust.Andrew.MTG.Player.Shared.Zone.Graveyard
                || newZone == Faust.Andrew.MTG.Player.Shared.Zone.InPlay
                || newZone == Faust.Andrew.MTG.Player.Shared.Zone.Graveyard)
            {
                if (card.IsFaceDown) { return "A Morphed Card"; }

                return card.Name;
            }
            else
            {
                return "A Card";
            }
        }
    }
}
